package br.com.localfloripa.controllers;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;

import br.com.localfloripa.dao.ServicoDao;
import br.com.localfloripa.entities.Servico;

@Named("servicoController")
@Scope("session")
public class ServicoController implements Serializable {

	private static final long serialVersionUID = -7421968879776044468L;
	private static final Logger log = LoggerFactory.getLogger(ServicoController.class);
	private final Integer pageSize = 10;
	
	@Inject
	@Named("servicoDao")
	private ServicoDao servicoDao;
	
	private LazyDataModel<Servico> lazyModelServico;
	private Servico servico;
	
	/** controla linha selecionada do dataTable **/
	private boolean selectedServico = false;
	
	
	public ServicoController() {
		
	}
	
	@PostConstruct
	public void loadModels() {
		lazyModelServico = new LazyDataModel<Servico>() {

			@Override
			public int getPageSize() {
				return pageSize;
			}

			@Override
			public int getRowCount() {
				return servicoDao.countListServicos().intValue();
			}

			@Override
			public List<Servico> load(int first, int pageSize, String sortField, SortOrder order, Map<String, String> filters) {
				List<Servico> servicos = null;
				try {
					servicos = servicoDao.listServicosPaginada(first, pageSize);
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}				
				return servicos;
			}
			
			@Override  
		    public Object getRowKey(Servico servico) {  
		        return servico.getId();  
		    } 
			
			@Override  
		    public Servico getRowData(String rowKey) {  
				
				if (rowKey != null && !rowKey.equals("null")) {
					List<Servico> servicos = (List<Servico>) getWrappedData();  
			          
			        for(Servico servico : servicos) {  
			            if(servico.getId().equals(Long.valueOf(rowKey)))  
			                return servico;  
			        }
				}  		          
		        return null;  
		    }  
		};
	}
	
	public String save() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		try {
			servicoDao.persist(this.servico);
			loadModels();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.info.confirm"), bundle.getString("manager.servico.create.confirm")));  
			return "/faces/manager/xhtml/servico/list.xhtml";
		} catch (Exception e) {
			log.error(e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.servico.create.error"))); 
			return "/faces/manager/xhtml/servico/create.xhtml";
		}
	}
	
	public String update() {
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		try {
			servicoDao.merge(this.servico);
			loadModels();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.info.confirm"), bundle.getString("manager.servico.update.confirm")));  
			return "/faces/manager/xhtml/servico/list.xhtml";
		} catch (Exception e) {
			log.error(e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.servico.update.error"))); 
			return "/faces/manager/xhtml/servico/update.xhtml";
		}
	}
	
	public String edit() {
		if (this.servico != null) {
			return "/faces/manager/xhtml/servico/update.xhtml";
		} else {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
					bundle.getString("messages.sumary.error"), bundle.getString("manager.servico.edit.error")));
			return "/faces/manager/xhtml/servico/list.xhtml";
		}
	}
	
	public String create() {		
		this.servico = new Servico();
		return "/faces/manager/xhtml/servico/create.xhtml";
	}
	
	public LazyDataModel<Servico> getLazyModelServico() {
		return lazyModelServico;
	}
	
	public void setLazyModelServico(LazyDataModel<Servico> lazyModelServico) {
		this.lazyModelServico = lazyModelServico;
	}
	
	public Servico getServico() {
		if (this.servico == null) {
			this.servico = new Servico();
		}
		return servico;
	}
	
	public void setServico(Servico servico) {
		this.servico = servico;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public boolean isSelectedServico() {
		return selectedServico;
	}

	public void onSelectedRow() {
		this.selectedServico =  true; 
	}
	
	public void onPaged() {
		this.selectedServico = false;
	}

}
